﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;

namespace WuxiWTO.Web
{
    public partial class RegisterORG : System.Web.UI.Page
    {
        WTODataContext ctx = new WTODataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                InitLoginTab();
                //BuildEnterpriseCategory();
                //BuildEnterpriseSale();
            }
        }

        private void InitLoginTab()
        {
            if (UserInfo.AllowUserRegister())
            {
                this.panLogin.Visible = true;
                this.panHidLogin.Visible = false;
            }
            else
            {
                this.panLogin.Visible = false;
                this.panHidLogin.Visible = true;
            }
        }
        #region Removed Codes
        //private void BuildEnterpriseCategory()
        //{
        //    this.ddlEnterpriseType.Items.Clear();
        //    Dictionary<string, string> dic = Utility.GetXML(Server.MapPath("\\metadate\\enterpriseCategory.xml"));
        //    foreach (var ite in dic)
        //    {
        //        ddlEnterpriseType.Items.Add(new ListItem(ite.Value, ite.Key));
        //    }
        //    ddlEnterpriseType.Items.Insert(0,new ListItem("--请选择--", ""));
        //}

        //private void BuildEnterpriseSale()
        //{
        //    this.ddlEnterpriseSale.Items.Clear();
        //    Dictionary<string, string> dic = Utility.GetXML(Server.MapPath("\\metadate\\enterpriseSale.xml"));
        //    foreach (var ite in dic)
        //    {
        //        ddlEnterpriseSale.Items.Add(new ListItem(ite.Value, ite.Key));
        //    }
        //    ddlEnterpriseSale.Items.Insert(0,new ListItem("--请选择--", ""));
        //}

        #endregion

        protected void btnOK_Click(object sender, EventArgs e)
        {
            #region 验证表单
            if (string.IsNullOrEmpty(this.txtLoginNameP.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "namef", "alert('用户名为必填项');", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtPasswordP.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "passf", "alert('密码为必填项');", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtEmailP.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "emailf", "alert('Email为必填项');", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtNameP.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "mingf", "alert('企业名称为必填项');", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtContact.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "liencef", "alert('联系人为必填项');", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtAddress.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "liencef", "alert('地址为必填项');", true);
                return;
            }
            #endregion
            try
            {
                supporter sup = ctx.supporters.SingleOrDefault(p => p.supporter_login_name == this.txtLoginNameP.Text.Trim());
                if (sup != null)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "dupname", "alert('用户名已经存在，请更改');", true);
                    return;
                }

                sup = ctx.supporters.SingleOrDefault(p => p.email == this.txtEmailP.Text.Trim());

                if (sup != null)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "dupemail", "alert('邮箱地址已经存在，请更改');", true);
                    return;
                }

                sup = new supporter();
                sup.supporter_login_name = this.txtLoginNameP.Text.Trim();
                sup.supporter_password = this.txtPasswordP.Text.Trim();
                sup.email = this.txtEmailP.Text.Trim();
                sup.enterprise_name = this.txtNameP.Text.Trim();
                sup.supporter_type = 'O';
                sup.contact = this.txtContact.Text.Trim();
                sup.position = this.txtPosition.Text.Trim();
                sup.telephone = this.txtTelephoneP.Text.Trim();
                sup.enterprise_address = this.txtAddress.Text.Trim();
                //sup.purpose = this.ddlPurposeP.Value;
                //sup.business_license = this.txtZhizhao.Text.Trim();
                //sup.org_code = this.txtOrgCode.Text.Trim();
                if (UserInfo.AllowRegValidate())
                {
                    sup.is_active = 'N';
                }
                else
                {
                    sup.is_active = 'Y';
                }
                
                sup.created_date = DateTime.Now;
                sup.update_date = DateTime.Now;
                //sup.enterprise_kind = char.Parse(this.ddlEnterpriseType.SelectedItem.Value);
                //sup.business_range = this.ddlEnterpriseSale.SelectedItem.Value;
                ctx.supporters.InsertOnSubmit(sup);
                ctx.SubmitChanges();
                ClientScript.RegisterStartupScript(this.GetType(), "direct", "alert('恭喜你注册成功，请等待管理员审核');location='login.aspx';", true);
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "err", "alert('注册过程出现异常，注册失败，请稍后再试');", true);
            }
        }
    }
}